//
//  UIScrollView+MGRefresh.m
//  Express
//
//  Created by lhongl on 2025/9/26.
//  Copyright © 2025 QM. All rights reserved.
//

#import "UIScrollView+MGRefresh.h"
#import <MJRefresh/MJRefresh.h>
@implementation UIScrollView (MGRefresh)

/// 刷新
/// - Parameter headerBlock: 刷新回调
- (void)initHeaderRefresh:(void(^)(void))headerBlock {
    if (self.mj_header.isRefreshing) {
        return;
    }
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.mj_footer resetNoMoreData];
        if (headerBlock) {
            headerBlock();
        }
    }];

    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"即将为您更新数据..." forState:MJRefreshStatePulling];
    [header setTitle:@"正在全力加载数据..." forState:MJRefreshStateRefreshing];
    [header setTitle:@"已完成数据更新..." forState:MJRefreshStateIdle];
    self.mj_header =header;
}

/// 加载
/// - Parameter footerBlock: 加载回调
- (void)initFooterRefresh:(void(^)(void))footerBlock {
    if (self.mj_footer.isRefreshing) {
        return;
    }
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (footerBlock) {
            footerBlock();
        }
    }];
    footer.arrowView.image = [UIImage new];
    [footer setTitle:@"即将为您更新数据..." forState:MJRefreshStatePulling];
    [footer setTitle:@"正在全力加载数据..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"正在全力加载数据..." forState:MJRefreshStateWillRefresh];
    [footer setTitle:@" " forState:MJRefreshStateIdle];
    [footer setTitle:@"亲,我是有底线的哦~~~" forState:MJRefreshStateNoMoreData];
    self.mj_footer = footer;
}

/// 刷新加载
/// - Parameters:
///   - headerBlock: 刷新回调
///   - footerBlock: 加载回调
- (void)initHeaderRefresh:(void(^)(void))headerBlock
            FooterRefresh:(void(^)(void))footerBlock {
    [self initHeaderRefresh:headerBlock];
    [self initFooterRefresh:footerBlock];
}

/// 开始刷新
- (void)startRefreshing {
    [self.mj_header beginRefreshing];
}

/// 结束刷新
- (void)finshRefreshing {
    if(self.mj_header.isRefreshing){
        [self.mj_header endRefreshing];
    }
    if(self.mj_footer.isRefreshing){
        [self.mj_footer endRefreshing];
    }

}

/// 隐藏footer
/// - Parameter hiddle: 状态
- (void)setFooterHidden:(BOOL)hiddle {
    [self.mj_footer setHidden:hiddle];
}

@end
